<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        function myAxios(options) {
            let { url, method = 'get', data, params } = options
            // 'http://ajax-api.itheima.net/api/robot?spoken=我给你送个包子？&name=werewr'
            if (method === 'get') {
                let arr = []
                for (let key in params) {
                    console.log(key)
                    console.log(params[key])
                    console.log(`${key}=${params[key]}`, '----------------')
                    arr.push(`${key}=${params[key]}`)
                }
                console.log(arr.join('&'))
                url += `?${arr.join('&')}`
                console.log(url)
            }
            return new Promise((resolve, reject) => {
                // 1.实例化异步对象
                const xhr = new XMLHttpRequest()
                // 2.设置请求的 地址 和方法
                xhr.open(method, url)
                xhr.setRequestHeader('content-type', 'application/json')
                // 3.注册回调函数 服务器响应内容回来之后触发
                xhr.addEventListener('load', function () {
                    // console.log(xhr.response)
                    resolve(JSON.parse(xhr.response))
                })
                // 4.发送请求
                xhr.send()
            })
        }
        myAxios({
            url: 'http://ajax-api.itheima.net/api/robot',
            method: 'get',
            params: {
                spoken: '我给你送个包子？',
                name: 'werewr',
            },
        }).then((res) => {
            console.log(res)
        })
    </script>
</body>

</html>